package org.hp.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.hp.bean.Patient;
import org.hp.service.patient.PatientService;
import org.hp.util.Layui;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class PatientController {

    @Resource
    PatientService patientService;
    Layui layui;
    @RequestMapping("/queryAll.do")
    public void queryAll(Patient patient, Integer page,Integer limit,HttpServletResponse response) throws IOException {
        PageHelper.startPage(page,limit);
        List<Map<String, Object>> list=patientService.queryAll(patient);
        PageInfo pageInfo=new PageInfo(list,limit);
        response.getWriter().write(JSON.toJSONString(layui.select(pageInfo)));
    }
    @RequestMapping("/del.do")
    public void del(@RequestParam int id, HttpServletResponse response) throws IOException {
        if (patientService.del(id)){
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        }
    }
    @RequestMapping("/queryDelAll.do")
    public void queryDelAll(Integer page,Integer limit,HttpServletResponse response) throws IOException {
        PageHelper.startPage(page,limit);
        List<Map<String, Object>> list=patientService.queryDelAll();
        PageInfo pageInfo=new PageInfo(list,limit);
        response.getWriter().write(JSON.toJSONString(layui.select(pageInfo)));
    }
    @RequestMapping("/del2.do")
    public void del2(@RequestParam int id, HttpServletResponse response) throws IOException {
        if (patientService.del2(id)){
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        }
    }
    @RequestMapping("/queryDoctorAll.do")
    public void queryDoctorAll(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list=patientService.queryDoctorAll();
        response.getWriter().write(JSON.toJSONString(layui.select(list)));
    }
    @RequestMapping("/insert.do")
    public void insert(Patient patient, HttpServletResponse response) throws IOException {
        if (patientService.insert(patient)){
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        }
    }
    @RequestMapping("/queryOne.do")
    public void queryOne(@RequestParam int  id, HttpServletResponse response) throws IOException {
        System.out.println("pid:"+id);
        List<Map<String, Object>> list=patientService.queryOne(id);
        response.getWriter().write(JSON.toJSONString(layui.select(list)));
    }
    @RequestMapping("/upd.do")
    public void upd(Patient patient, HttpServletResponse response) throws IOException {
        System.out.println(patient);
       if (patientService.upd(patient)){
           response.getWriter().write(JSON.toJSONString(layui.update(0)));
       }
    }
}
